// pages/store/store.js
const app = getApp();
import api from '../../../utils/util.js';
Page({
  data: {
    ent: app.globalData.ent,
    statusBarHeight: app.globalData.statusBarHeight,
    fullHeight: app.globalData.fullcontain,
    navmess:{
      navtitle: "商店",
      backhide: 1,
      topheight: 1
    },
    curIdx: 2,
    currentTab: 0,
    height: 0,   //单个main高度
    widHeight: 0,  //swiper高度
    ranktype: false, //排列类型

    // renderList: [[],[],[]], // 分页数据渲染
    pageNum: [1,1,1], //当前页码
    pageSize: 6, 
    pages: 0, // 总页码
    loading: [false,false,false], 
    noMore:  [false,false,false], 
    loadingFailed: [false,false,false] 
  },
  pageto: function(e){
    var url = e.currentTarget.dataset.url,
        data = e.currentTarget.dataset.datobj;
    app.goto(url,data);
  },
  // 滑动切换tab
  bindChange: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  // 点击tab切换
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  // 点击切换排序方式
  changeRank:function(){
    var that = this;
    that.setData({
      ranktype: !that.data.ranktype
    })
  },
  // 数据请求(初始化,刷新)
  loadData: function(){
    this.setData({
      pageNum: [1,1,1],
      loading: [false,false,false], 
      noMore:  [false,false,false], 
      loadingFailed: [false,false,false] 
    })
    for(var i = 0;i < this.data.urlArr.length;i++){
      this.getData(false,i,this.data.urlArr[i]); //请求数据
    }
    console.log("刷新：",this.data.renderList)
  },
  //到达底部
  scrollToLower: function (e) {
    var loading = this.data.loading,
        noMore = this.data.noMore,
        loadingFailed = this.data.loadingFailed,
        index = this.data.currentTab,
        pageNum = this.data.pageNum;
    if (!loading[index] && !noMore[index]){
      loading[index] = true;
      pageNum[index]++;
      this.setData({
        loading: loading,
        pageNum: pageNum
      })
      this.getData(true,index,this.data.urlArr[index]); //请求数据
    }
  },
  //请求数据(是否分页,swiper索引,url)
  getData(isPage,index,url) {
    var loading = this.data.loading,
        noMore = this.data.noMore,
        loadingFailed = this.data.loadingFailed,
        pageNum = this.data.pageNum;
    var that = this;
    console.log("原本未拼接数据：", that.data.renderList[index]);
    let params = {
      "ent": that.data.ent,
      "pageNum": pageNum[index],
      "pageSize": that.data.pageSize
    }
    let renderList = that.data.renderList;
    //请求
    api.post(url,params).then(res =>{
      // var index = that.data.currentTab;
      loading[index] = false;
      that.setData({
        loading: loading
      })
      console.log("新增数据：",res)
      if (res.code == 200) {
        if (isPage) {
          //下一页的数据拼接在原有数据后面
          renderList[index] = renderList[index].concat(res.result.list);
          console.log("拼接后：",that.data.renderList);
          that.setData({
            renderList: renderList
          })
          //如果当前为尾页
          if (res.result.isLastPage) {
            noMore[index] = true;
            that.setData({
              noMore: noMore
            })
          }else{
            noMore[index] = false;
            that.setData({
              noMore: noMore
            })
          }
        } else {
          //第一页数据直接赋值
          renderList[index] = res.result.list;
          that.setData({
            renderList: renderList
          })
        }
        that.refreshView.stopPullRefresh();
      }
    }).catch(res => {
      console.log(res)
      //返回请求失败信息
      loading[index] = false;
      loadingFailed[index] = true;
      that.setData({
        loading: loading,
        loadingFailed: loadingFailed
      })
      return false;
    })
  },
  // top下拉刷新 
  //触摸开始
  handletouchstart: function (event) {
    this.refreshView.handletouchstart(event)
  },
  //触摸移动
  handletouchmove: function (event) {
    this.refreshView.handletouchmove(event)
  },
  //触摸结束
  handletouchend: function (event) {
    this.refreshView.handletouchend(event)
  },
  //触摸取消
  handletouchcancel: function (event) {
    this.refreshView.handletouchcancel(event)
  },
  //页面滚动
  onPageScroll: function (event) {
    this.refreshView.onPageScroll(event)
  },
  onPullDownRefresh:function(){
    this.loadData();
  },
  _pullState: function(e){
    // console.log(e.detail) 
  },
  // 代参详情页跳转
  takegoodto: function(e){
    var url = e.currentTarget.dataset.url;
    if(this.data.isLogin){
      wx.navigateTo({
        url: url + "?sign=" + JSON.stringify(e.currentTarget.dataset.sign),
      })
    }else{
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  projectto: function(e){
    var url = e.currentTarget.dataset.url;
    if(this.data.isLogin){
      wx.navigateTo({
        url: url + "?sign=" + JSON.stringify(e.currentTarget.dataset.sign),
      })
    }else{
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 刷新组件
    this.refreshView = this.selectComponent("#refreshView");
    var urlArr = ["/mini/goods/productSaleList","/mini/goods/projectSaleList","/mini/goods/treatmentSaleList"];
    this.setData({
      urlArr: urlArr
    })
    Promise.all(api.postAll(urlArr,{
      "ent": this.data.ent,
      "pageNum": 1,
      "pageSize": 6
    })).then(res => {
        var temp = [];
        for(var i=0;i<res.length;i++){
          temp.push(res[i].result.list);
        }
        this.setData({
          renderList: temp
        })
        console.log("初始化：",this.data.renderList);
    }).catch(err => {
        console.log(err)
    });
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    // this.setData({
    //   topNum: 0
    // })
    this.setData({
      isLogin: app.checkLogin()
    })
    var urlArr = that.data.urlArr;
    if(!that.data.renderList){
      Promise.all(api.postAll(urlArr,{
        "ent": this.data.ent,
        "pageNum": 1,
        "pageSize": this.data.pageSize
      })).then(res => {
          var temp = [];
          for(var i=0;i<res.length;i++){
            temp.push(res[i].result.list);
          }
          this.setData({
            renderList: temp
          })
          console.log("初始化：",this.data.renderList);
      }).catch(err => {
          console.log(err)
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})